//
//  YXScrollLabel.m
//  ScrollLabel
//
//  Created by rimi on 16/3/14.
//  Copyright © 2016年 YXXiang. All rights reserved.
//

#import "YXScrollLabel.h"

@interface YXScrollLabel ()

@property (nonatomic, assign, nullable) id <YXScrollLabelDelegate> delegate;//hold on delegate by private.
@property (nonatomic, strong) NSArray <NSString *> * texts;//save text of all.
@property (nonatomic, strong) UIFont * textFont;//save text font.
@property (nonatomic, assign) NSTimeInterval duration;//save duration.
@property (nonatomic, assign) YXScrollAnimationTimeOption timeFunction;//save time function.
@property (nonatomic, assign) YXScrollDirection direction;//save direction.
@property (nonatomic, strong) UILabel * showLabel;//show all data.
@property (nonatomic, assign) CGRect showLableFrame;//save label frame.
@property (nonatomic, assign) CGFloat oneTextHeight;//according to font ,get height of text.

@end

@implementation YXScrollLabel

//这里加个代理应该用谁？为什么，
- (instancetype)initWithFrame:(CGRect)frame delegate:(id<YXScrollLabelDelegate>)delegate {
    if ([super initWithFrame:frame]) {
        self.clipsToBounds = YES;
        self.delegate = delegate;
    }
    return self;
}
#pragma mark Getter
- (UILabel *)showLabel {
    if (!_showLabel) {
        _showLabel = [[UILabel alloc] init];
        _showLabel.numberOfLines = 0;
        _showLabel.text = [self attributedStringForShowLabel];
        _showLabel.frame = self.showLableFrame;
        _showLabel.font = self.textFont;
        _showLabel.textColor = self.textColor;
        _showLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _showLabel;
}
- (CGRect)showLableFrame {
    if (self.direction == Top) {
        return CGRectMake(0, 0, self.bounds.size.width, self.oneTextHeight*self.texts.count);
    }else {
        return CGRectMake(0, -self.oneTextHeight*(self.texts.count-1), self.bounds.size.width, self.oneTextHeight*self.texts.count);
    }
}
//字体颜色
- (UIColor *)textColor {
    if (!_textColor) {
        _textColor = [UIColor blackColor];
    }
    return _textColor;
}
#pragma mark through delegate get data
- (void)configrationData {
    self.texts = [self.delegate dataOfScrollValue:self];
    if ([self.delegate respondsToSelector:@selector(fontForText:)]) {
        self.textFont = [self.delegate fontForText:self];
    }else {
        self.textFont = [UIFont systemFontOfSize:15];
    }
    if ([self.delegate respondsToSelector:@selector(scrollingDuration:)]) {         //设置标签控件到一些属性，可以通过代理设置，如果没有设置，则为默认值
        self.duration = [self.delegate scrollingDuration:self];
    }else {
        self.duration = 1.0f;
    }
    if ([self.delegate respondsToSelector:@selector(timeFunctionOfScrollAnimation:)]) {
        self.timeFunction = [self.delegate timeFunctionOfScrollAnimation:self];
    }else {
        self.timeFunction = CurveEaseInOut;
    }
    if ([self.delegate respondsToSelector:@selector(directionForScrollLabel:)]) {
        self.direction = [self.delegate directionForScrollLabel:self];
    }else {
        self.direction = Top;
    }
}
#pragma mark functions
- (NSString *)attributedStringForShowLabel {
    NSMutableString * str = [NSMutableString string];
    for (int i = 0; i < self.texts.count; ++ i) {
        if (i == self.texts.count-1) {
            [str appendFormat:@"%@",self.texts[i]];
        }else {
            [str appendFormat:@"%@\n",self.texts[i]];
        }
    }
    return str;
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];           //每次父视图更新就会执行 这句话是添加控件到主视图么？  （在注视图中 执行self.view addsubView:  前会执行这个）
    [self configrationData];
    //to get heigth
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:self.textFont, NSFontAttributeName,nil];       //这里的点语法是怎么得到值的 textFont
    self.oneTextHeight = [self.texts.firstObject boundingRectWithSize:self.bounds.size options:NSStringDrawingUsesLineFragmentOrigin attributes:tdic context:nil].size.height;
    //change frame
    self.bounds = CGRectMake(0, 0, self.bounds.size.width, self.oneTextHeight);
    [self addSubview:self.showLabel];
}
- (void)didMoveToSuperview {
    [super didMoveToSuperview];
    //set timefunction
    UIViewAnimationOptions options;
    switch (self.timeFunction) {
        case CurveEaseIn:
            options = UIViewAnimationOptionCurveEaseInOut;
            break;
        case CurveEaseOut:
            options = UIViewAnimationOptionCurveEaseOut;
            break;
        case  CurveEaseInOut:
            options = UIViewAnimationOptionCurveEaseInOut;
            break;
        case CurveLinear:
            options = UIViewAnimationOptionCurveLinear;
            break;
        default:
            break;
    }
    //aniamtion
    switch (self.direction) {
        case Top:
            [self animationTopWithOption:options];
            break;
        case Bottom:
            [self animationBottomWithOption:options];
            break;
        default:
            break;
    }
}
- (void)animationTopWithOption:(UIViewAnimationOptions)option {
    [UIView animateWithDuration:self.duration delay:0 options:option animations:^{
        self.showLabel.frame = CGRectMake(0, -self.oneTextHeight*(self.texts.count-1), self.bounds.size.width, self.oneTextHeight*self.texts.count);//
//        self.showLabel.transform = 
    } completion:nil];
}
- (void)animationBottomWithOption:(UIViewAnimationOptions)option {
    [UIView animateWithDuration:self.duration delay:0 options:option animations:^{
        self.showLabel.frame = CGRectMake(0, 0, self.bounds.size.width, self.oneTextHeight*self.texts.count);
    } completion:nil];
}

@end
